package com.leadingsoft.pwxk.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.leadingsoft.pwxk.excel.ExcelUtil;
import com.leadingsoft.pwxk.model.RiverBasinRegionRelation;
import com.leadingsoft.pwxk.repository.RegionCodeAllRepository;
import com.leadingsoft.pwxk.repository.RiverBasinRegionRelationRepository;
import com.leadingsoft.pwxk.service.RiverBasinRegionService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RiverBasinRegionServiceImpl implements RiverBasinRegionService {

	@Autowired
	RiverBasinRegionRelationRepository rbRepository;

	@Autowired
	RegionCodeAllRepository regionCodeAllRepository;

	@Override
	public void importData(MultipartHttpServletRequest request, String type) throws IOException {
		Iterator<String> filenames = request.getFileNames();
		while (filenames.hasNext()) {
			MultipartFile file = request.getFile(filenames.next());
			// 解析数据为map <行号,数据内容>
			List<Map<Integer, List<String>>> datas = ExcelUtil.parseExcel(file, 0, 1);
			if (!type.equals("TJ")) {
				dealData(datas);
			} else {
				dealTJData(datas);
			}

		}
	}

	private void dealData(List<Map<Integer, List<String>>> datas) {
		for (Map<Integer, List<String>> tempMap : datas) {
			String riverBasinName = "";
			String regionName = "";
			for (List<String> t : tempMap.values()) {
				if (StringUtils.isNotBlank(t.get(0))) {
					riverBasinName = t.get(0);
				}
				if (StringUtils.isNotBlank(t.get(1))) {
					regionName = t.get(1);
				}
				String regionDetailName = t.get(2);
				RiverBasinRegionRelation rr = new RiverBasinRegionRelation();
				rr.setRegionName(regionName);
				rr.setRiverBasinName(riverBasinName);
				rr.setRegionDetailName(regionDetailName);
				List<RiverBasinRegionRelation> all = rbRepository.findByRegionNameAndRegionDetailName(regionName,
						regionDetailName);
				if (all.size() > 0) {
					log.info(riverBasinName + regionName + "->" + regionDetailName + "已经被其他流域占用");
				}
				if (all.size() == 0) {
					rbRepository.save(rr);
				}
			}
		}
	}

	private void dealTJData(List<Map<Integer, List<String>>> datas) {
		for (Map<Integer, List<String>> tempMap : datas) {
			String riverBasinName = "沱江";
			String regionName = "";
			for (List<String> t : tempMap.values()) {
				if (StringUtils.isNotBlank(t.get(0))) {
					regionName = t.get(0);
				}
				String regionDetailName = t.get(1);
				RiverBasinRegionRelation rr = new RiverBasinRegionRelation();
				rr.setRegionName(regionName);
				rr.setRiverBasinName(riverBasinName);
				rr.setRegionDetailName(regionDetailName);
				List<RiverBasinRegionRelation> all = rbRepository.findByRegionNameAndRegionDetailName(regionName,
						regionDetailName);
				if (all.size() > 0) {
					log.info(riverBasinName + regionName + "->" + regionDetailName + "已经被其他流域占用");
				}
				if (all.size() == 0) {
					rbRepository.save(rr);
				}
			}
		}
	}

	@Override
	public List<String> getList() {
		List<RiverBasinRegionRelation> findAll = rbRepository.findAll();
		Map<String, Object> map = new HashMap<String, Object>();
		findAll.forEach(t -> {
			map.put(t.getRiverBasinName(), t.getRiverBasinName());
		});
		List<String> result = new ArrayList<String>();
		map.forEach((key, val) -> {
			result.add(key);
		});
		return result;
	}

}
